/**
 * Created by zhoumingrui on 2017/6/11.
 */
'use strict'

var fs = require('fs')
var turf = require('turf')

var points = [
    [116.4879, 39.957764],
    [116.489014, 39.955012],
    [116.489507, 39.95335],
    [116.489893, 39.950538],
    [116.48985, 39.945323],
    [116.489735, 39.93385]
]

var featureCollection = {
    "type": "FeatureCollection",
    "features": []
}

for (let i = 0; i < points.length - 1; i++) {
    var start = points[i]
    var end = points[i + 1]

    var from = {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Point",
            "coordinates": [-75.343, 39.984]
        }
    };

    var from = turf.point(start);
    var to = turf.point(end);

    var units = "kilometers";

    var distance = turf.distance(from, to, units) * 1000;

    var seg = distance / 100;
    var dLng = (end[0] - start[0]) / seg;
    var dLat = (end[1] - start[1]) / seg;

    for (let j = 0; j < seg; j++) {
        featureCollection.features.push({
            "type": "Feature",
            "properties": {
                "id": "00" + j,
                "heading": 90,
                "size": 10,
                "name": "tree"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    start[0] + dLng * j, start[1] + dLat * j
                ]
            }
        })
    }
}

fs.writeFile('./out.json', JSON.stringify(featureCollection))